package com.twohand.admin.Controller;

import com.twohand.admin.Annotation.Permission;
import com.twohand.admin.Dto.AddUserDto;
import com.twohand.admin.Dto.ChangePwdDto;
import com.twohand.admin.Dto.ModifyUserDto;
import com.twohand.admin.Dto.UserListQueryDto;
import com.twohand.admin.Service.UserService;
import com.twohand.admin.Util.PageResponse;
import com.twohand.admin.Vo.UserListVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.security.NoSuchAlgorithmException;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;
//    @Autowired
//    RedisCache redisCache;
    @Permission(value = "user:list")
    @GetMapping
    PageResponse<UserListVo> getAllUser(UserListQueryDto userListQueryDto){
//        String token = JwtUtil.getCurrentUser();
//        User user = redisCache.getCacheObject(token);
        return userService.getAllUser(userListQueryDto);
    }
    @Permission(value = "user:add")
    @PostMapping
    Boolean addUser(@RequestBody AddUserDto user){
        return userService.insertUser(user);
    }

    @Permission(value = "user:edit")
    @PutMapping
    boolean updateUser(@RequestBody ModifyUserDto user){

        return userService.updateUser(user);
    }

    @Permission(value = "user:delete")
    @DeleteMapping("/{id}")
    Boolean deleteUser(@PathVariable Integer id){
        return userService.deleteUserById(id);
    }

    @PostMapping("/avatar/upload")
    Object uploadAvatar(@RequestParam MultipartFile file){
        return userService.uploadAvatar(file);
    }
    @PostMapping("/changePwd")
    Boolean changePwd(@RequestBody ChangePwdDto changePwdDto){
        return userService.changPassword(changePwdDto);
    }
    @Permission(value = "user:reset")
    @PostMapping("/resetPwd/{id}")
    Boolean resetPwd(@PathVariable Integer id) throws NoSuchAlgorithmException {
        return userService.resetPwd(id);
    }
}
